package com.xiaochong.bitong.api.controller;

import com.xc.logclient.utils.LogTrace;
import com.xiaochong.bitong.api.config.H5Url;
import com.xiaochong.bitong.api.constant.ReturnCode;
import com.xiaochong.bitong.api.service.KnowledgeService;
import com.xiaochong.bitong.api.service.ReportService;
import com.xiaochong.bitong.api.utils.ApiVersion;
import com.xiaochong.service.common.config.oss.OSSTool;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.mapper.KnowledgeRoomMapper;
import com.xiaochong.tir.common.data.po.KnowledgeRoom;
import com.xiaochong.tir.common.data.po.SyResearchReport;
import com.xiaochong.tir.common.data.vo.ApiResultVo;
import com.xiaochong.tir.common.data.vo.CurrencyQuotationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 币种
 * @author MengXiangDong
 */
@RestController
@Api(value = "知识讲堂")
@RequestMapping(value = ApiVersion.API_V1_PREFIX+"/knowledge")
public class KnowledgeController {

	@Autowired
	private RedisServer redisServer;

	@Autowired
	private KnowledgeService knowledgeService;

	@Autowired
	private KnowledgeRoomMapper knowledgeRoomMapper;

	@Autowired
	private OSSTool ossTool;

	/**
	 * 知识讲堂
	 */
	@ApiOperation(value = "知识讲堂" , notes = "知识讲堂")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageIndex", value = "页数", required = true, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "条数", required = true, dataType = "Integer", paramType = "query")
	})
	@PostMapping("knowledgeRoomList")
	public ApiResultVo<List<KnowledgeRoom>> knowledgeRoomList(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,
														  @RequestParam(value = "pageSize", required = true) Integer pageSize
																 ){
		CurrencyQuotationVo currencyQuotationVo = new CurrencyQuotationVo();
		currencyQuotationVo.setPageIndex( pageIndex );
		currencyQuotationVo.setPageSize( pageSize );
		ApiResultVo apiResultVo=knowledgeService.knowledgeRoomList(currencyQuotationVo);
		return apiResultVo;
	}

	/**
	 * 知识讲堂详情
	 */
	@ApiOperation(value = "知识讲堂详情" , notes = "知识讲堂详情")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer", paramType = "query")
	})
	@PostMapping("knowledgeRoomDetail")
	public ApiResultVo<KnowledgeRoom> knowledgeRoomDetail(@RequestParam(value = "id", required = true) Integer id
	){
		ApiResultVo apiResultVo = new ApiResultVo();
		apiResultVo.setTradeId( LogTrace.getTrace().getTraceId() );
		try{
			KnowledgeRoom knowledgeRoom = new KnowledgeRoom();
			knowledgeRoom.setId( id );
			knowledgeRoom=knowledgeRoomMapper.selectByEntity( knowledgeRoom );
			if(knowledgeRoom!=null && StringUtils.isNotBlank(knowledgeRoom.getArticleImg())){
				if(!knowledgeRoom.getArticleImg().contains( "http" )){
					String imgUrl=ossTool.getOSSUrl( knowledgeRoom.getArticleImg() );
					knowledgeRoom.setArticleImg( imgUrl );
				}
			}
			knowledgeRoom.setArticleContent( knowledgeRoom.getArticleContent().replace("OSSAccessKeyId" , "" ) );
			apiResultVo.setData( knowledgeRoom );
			apiResultVo.setCode( ReturnCode.SUCCESS );
		}catch (Exception e){
			apiResultVo.setCode( ReturnCode.ERROR );
			e.printStackTrace();
			LogTrace.error("知识讲堂详情",e);

		}

		return apiResultVo;
	}
}
